<?php
namespace app\home\controller;

use app\home\model\BusinessModel;
use app\home\model\CommonModel;
use think\Controller;
use think\Request;
use think\Db;

class Business extends Controller{
    public function __construct(Request $request = null)
    {
        $result = $this->_getBid();
        if(!$result){
            $this->success('未登录', 'index/login');
        }
    }
   public function index(){
       $bid = $this->_getBid();
       $field = 'bid,name,address,logo,phone';
       $result = BusinessModel::instance()->buslist($bid,$field);
       foreach($result as &$val){
           $val['logo'] = uploadpath('business',$val['logo']);
       }
//       var_dump($result);exit;
       return view('business/index',['data'=>$result]);
   }


    public function edit(Request $request){
        $id = $request->get('id');
        $result = BusinessModel::instance()->busonelist($id);
        $result['logo'] = uploadpath('business',$result['logo']);
        return view('business/edit',['data'=>$result]);
    }



    public function editer(Request $request){
        $pic = CommonModel::instance()->upload('business');
        $data = $request->post();
        $data['logo'] = $pic;
        $bid =  $data['bid'];
        if(empty($data['content'])){$this->error('公司简介不能为空!');}
        $result = BusinessModel::instance()->buseditlist($bid,$data);
        if($result){
            $this->success('修改成功', 'business/index');
        }else{
            $this->error('修改失败');
        }
    }
    /*
     * 海报图显示
     */
    public function haibao(){
        $bid = $this->_getBid();
        $res = Db::name('haibao')->where('bid',$bid)->find();
        if ($res){
            $res['photo'] = uploadpath('business',$res['photo']);
        }else if (!$res){
            $res['bid'] = $bid;
            $res['photo'] = 'http://www.jq22.com/demo/imgUp201703281022/img/a11.png';
        }
        return view('business/haibao',['data'=>$res]);
    }
    /*
     * 海报图设置
     */
    public function hbedit(Request $request){
        $data = $request->post();
        $pic = CommonModel::instance()->upload('business');
        $data['photo'] = $pic;
        $datas['photo'] = $pic;
        $bid =  $data['bid'];
        $res = Db::name('haibao')->where('bid',$bid)->select();
        if (!$res){
            $result = Db::name('haibao')->where('bid',$bid)->insert($data);
        }else{
            $result = Db::name('haibao')->where('bid',$bid)->update($datas);
        }
        if($result){
            $this->success('修改成功', 'business/haibao');
        }else{
            $this->error('修改失败');
        }
    }
}